package com.project.internship.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.internship.entity.Student;
import com.project.internship.mapper.StudentMapper;
import com.project.internship.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.internship.utils.UserUtils;
import com.project.internship.vo.StudentDeptMajorClassVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2020-04-07
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    StudentMapper studentMapper;

    @Override
    public IPage<Student> getAllStudents(Page<Student> page, String keyword) {
        return studentMapper.getAllStudents(page, UserUtils.getCurrentUser().getDepartmentId(), keyword);
    }

    @Override
    public Integer addStudent(Student student) {
        student.setDepartmentId(UserUtils.getCurrentUser().getDepartmentId());
        return studentMapper.insert(student);
    }

    @Override
    public Integer updateStudent(Student student) {
        student.setDepartment(null);
        student.setMajor(null);
        student.setAClass(null);
        return studentMapper.updateById(student);
    }

    @Override
    public Integer deleteStudent(Long id) {
        return studentMapper.deleteById(id);
    }

    @Override
    public Integer deleteStudents(Long[] ids) {
        return studentMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<Student> getStudents(Integer id) {
        List<Student> list =  studentMapper.getStudents(id);
        for (Student student : list) {
            String studentId = student.getStudentId();
            String name = student.getName();
            name = studentId + " " + name;
            student.setName(name);
        }

        return list;
    }

}
